<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/10
 * Time: 10:38
 */

namespace app\api\controller;

use app\api\library\SecretSys;
use app\common\controller\Api;
use app\common\model\Config;
use app\api\Service\GameService;
use app\common\model\LotteryPvp;
use think\Hook;
use think\Request;
use app\api\Service\UserService;
use app\api\Service\WebsocketService;
use think\Queue;
use fast\Http;

class Pvp extends Api
{

    protected $noNeedLogin = ['index','current','histories'];
    protected $noNeedRight = ['*'];
    public $game = 'pvp';
    public $userService;
    public $websocket;
    protected $url;
    protected $roomId= 3;

    public function _initialize()
    {
        $this->userService = new UserService();
        $this->websocket   = new WebsocketService();
        $this->url = SecretSys::WEBSOCKET_HTTP_URL;
        parent::_initialize();
    }

    /**
     * 赔率
     * @param GameService $gameService
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(GameService $gameService)
    {
        $pvpodds = $gameService->getGameOdds('pvpodds');
        if( $pvpodds ){
            $this->success('成功',['odds'=>$pvpodds]);
        }else{
            $this->error('失败');
        }

    }

    /**
     * 当前开奖
     * @param GameService $gameService
     */
    public function current(GameService $gameService)
    {
        $current = LotteryPvp::with('gamepvp')->where('status',1)->order('index','desc')->find();
        $this->success('成功', [$current]);
    }

    /**
     * 开奖历史
     * @param GameService $gameService
     */
    public function histories(GameService $gameService)
    {
        $lotteries = $gameService->getLotteries($this->game);
        $this->success('成功', [$lotteries]);
    }

    /**
     * 预计收入
     * @param Request $request
     * @param GameService $gameService
     */
    public function estimates(Request $request,GameService $gameService)
    {
        if( $request->isPost() ){
            $data = $request->post();
            $res = $gameService->getEstimates('pvpodds',$data);
            $this->success('成功',$res);
        }

        $this->error('请求失败');

    }

    /**
     * 投注--删除（聊天室推送）
     * @param Request $request
     * @param GameService $gameService
     */
    public function betting_del(Request $request,GameService $gameService)
    {
        $first = mb_substr($this->auth->nickname,0,1,'UTF-8');
        $last = mb_substr($this->auth->nickname,-1,1,'UTF-8');
        $nickname = $first.'****'.$last;
        if( $request->isPost() ){
            $data = $request->post();
            $money = (float)$this->auth->wallet->money;
            $levelAuth = $gameService->checkRoomAuth($money);
            if( !in_array($data['roomlevel'],$levelAuth) ){ //检测房间权限
                $this->error('权限不足！');
            }
            if( $data['roomlevel'] == 'general' && $data['allmoney'] >= 10000 ){
                $this->error('已超出普通厅投注上限！');
            }

            $pvpopen = Config::where('group','basic')->where('name','pvpopen')->value('value');
            if ( !$pvpopen ){
                $this->error('游戏已封盘');
            }
            $res = $this->userService->betting($data);
            if( $res['code'] != 200 ){
                $this->error($res['msg']);
            }else{
                $content = [];
                foreach ( $data as $k=>$v ){
                    if( set_type_name($k) && $v > 0){
                        $content = json_encode([$k=>$v]);
                    }
                }
                $roomId = isset($data['roomId']) && !$data['roomId'] ? $data['roomId'] : 3;
                $messageData = [
                    'type'      => 'bettoclient',
                    'info'       => [
                        'user_id'   =>$this->auth->id,
                        'avatar'    => $this->auth->avatar,
                        'username'  => $this->auth->username,
                        'nickname'  => $nickname,
                        'content'   => $content,
                        'roomId'    => $roomId,
                        'room_level' => $data['roomlevel'],
                        'term'      => $data['sequencenum'],
                        'date'      => date('Y-m-d H:i:s',time())
                    ]
                ];
                Http::post($this->url,$messageData);
                $this->success('成功',$res);
            }

        }
        $this->error('请求失败');
    }
    /**
     * 投注
     * @param Request $request
     * @param GameService $gameService
     */
    public function betting(Request $request,GameService $gameService)
    {
        if( $request->isPost() ){
            $data = $request->post();
            $pvpopen = Config::where('group','basic')->where('name','pvpopen')->value('value');
            if ( !$pvpopen ){
                $this->error(['code'=>206,'msg'=>'游戏已封盘']);
            }
            $res = $this->userService->betting($data);
            if( $res['code'] != 200 ){
                $this->error($res);
            }else{

                $this->success('成功',$res);
            }
        }
        $this->error('请求失败');
    }
}